package logic

import (
	"encoding/xml"
	"github.com/gin-gonic/gin"
	"reflect"
)

type XmlInput struct {
	XMLName xml.Name `xml:"request"`
}

type RestLogic interface {
	Clone(ctx *gin.Context) RestLogic
	LoadInput() (input interface{}, err error)
	LoadInput4Update(where map[string]interface{}) (input interface{}, version uint64, err error)
	Add(input interface{}) (id uint64)
	Update(version uint64, input interface{}) (rows int64)
	Delete(id uint64) (rows int64)
	UpOrDown(id uint64, value uint8) (rows int64)
	View(where map[string]interface{}) (info interface{}, exist bool)
	Index(page int64, pageSize int64, where map[string]interface{}, orders []string) (pageData map[string]interface{})
}

func Input2Map(input interface{}) map[string]interface{} {
	val := reflect.ValueOf(input)
	data := map[string]interface{}{}
	fieldsNum := val.NumField()
	for index := 0; index < fieldsNum; index++ {
		field := val.Type().Field(index)
		fieldName := field.Tag.Get("field")
		if fieldName == "" {
			fieldName = field.Tag.Get("json")
		}
		if fieldName != "" && fieldName != "id" {
			data[fieldName] = val.Field(index).Interface()
		}
	}

	return data
}
